﻿// ActionScript file
package {
	
	
	import fl.controls.*;
	import fl.controls.Button;
	import fl.controls.List;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.core.*;
	import fl.data.*;
	import fl.data.DataProvider;
	import fl.events.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.engine.EastAsianJustifier;
	
	
	
	
	public class Lobby extends MovieClip{
		
		//private var myLobby = null;
		
		public var _userList:List;
		public var _showBuddyList:Button;
		public var _showCurrUserList:Button;
		
		public var _zoneList:List;
		public var _roomList:List;
		
		public var _quickJoin:Button;
		public var _joinRoom:Button;
		public var _createRoom:Button;
		
		public var _chatSend:Button;
		public var _chatInput:TextInput;
		public var _chatBox:TextArea;
		
		public var _userInfo:Button;
		public var _setBtn:Button;
		public var _userTip:MovieClip;
		
		public var _userInfoPanel:MovieClip;
		public var _messagePanel:MovieClip;
		public var _createRoomPanel:MovieClip;
		public var _passwordPanel:MovieClip;
		
		private var _currentSelectUserName:String;
		private var _currentSelectGameId:int;
		private var _currentSelectGameName:String;
		private var _currentSelectGamePassword:Boolean;
		
		private var _createRoomPasswordEnable:Boolean = false;
		
		public function Lobby(){
			trace("1");
			_userList = userList.useListPanel;
			_showBuddyList = userList.buddyList;
			_showCurrUserList = userList.currentUser;
			 
			_userTip = userTip;
			_userTip.visible = false;
			
			_userInfoPanel = userInfoPanel;
			_userInfoPanel.visible = false;
			_userInfo = userinfo;
			
			_messagePanel = messagePanel;
			_messagePanel.visible = false;
			
			_passwordPanel = passwordPanel;
			_passwordPanel.visible = false;
			
			_createRoomPanel = createRoomPanel;
			_createRoomPanel.visible = false;
			 
			_zoneList = zoneList.gameLists;
			_roomList = roomList.aDg;
			 
			_quickJoin = quickJoin;
			_joinRoom = joinRoom;
			_createRoom = createRoom;
			 
			_chatSend = chatBox.sendBtn;
			_chatInput = chatBox.textInput;
			_chatBox = chatBox.chatOutput;
			 
			//_userList.addEventListener(MouseEvent.MOUSE_OUT, onUserUnSelect );
		    _userList.addEventListener(Event.CHANGE, onUserSelect);
			//_userList.cell.addEventListener(MouseEvent.MOUSE_OVER,onUserSelect);
			//_userList.cell.addEventListener(MouseEvent.MOUSE_OUT,onUserUnSelect);
			
			_showBuddyList.addEventListener(MouseEvent.CLICK, updateBuddyList);
			_showCurrUserList.addEventListener(MouseEvent.CLICK, updateCurrUserList);
			trace("7");
			_zoneList.addEventListener(Event.CHANGE, onGameSelect);
			_roomList.addEventListener(Event.CHANGE, onRoomSelect);
			trace("8");
			_quickJoin.addEventListener(MouseEvent.CLICK, onQuickJoin);
			_joinRoom.addEventListener(MouseEvent.CLICK,onJoinRoom);
			_createRoom.addEventListener(MouseEvent.CLICK,onCreateRoom);
			trace("9");
			_chatSend.addEventListener(MouseEvent.CLICK, chatSend);
			_userInfo.addEventListener(MouseEvent.CLICK, getMyInfo);
			
			
			_userInfoPanel.close.addEventListener(MouseEvent.CLICK, userInfoPanelClose);
			_userInfoPanel.addBuddy.addEventListener(MouseEvent.CLICK,userAddBuddy);
			_userInfoPanel.removeBuddy.addEventListener(MouseEvent.CLICK,userRemoveBuddy);
			_userInfoPanel.setInfo.addEventListener(MouseEvent.CLICK, setUserInfo);
			trace("10");
			
			
			

			
		} 
		
		private function messageClose(e:MouseEvent):void{
			_messagePanel.btnclose.removeEventListener(MouseEvent.CLICK, messageClose);
			_messagePanel.visible = false;
		}
		
		private function userInfoPanelClose(e:MouseEvent):void{
			_userInfoPanel.visible = false;
		}
		
		private function userAddBuddy(e:MouseEvent):void{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.ADDBUDDY);
			dispatchEvent(event);
		}
		
		private function userRemoveBuddy(e:MouseEvent):void{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.REMOVEBUDDY);
			dispatchEvent(event);
		}
		
		private function setUserInfo(e:MouseEvent):void{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.SETUSERINFO);
			event.param = new Array();
			event.param[0] = _userInfoPanel.username.text;
			if( _userInfoPanel.gender.text == "男"){
				event.param[1] = 0;
			}else 
				event.param[1] = 1;
			event.param[2] = _userInfoPanel.state.text;
			
			dispatchEvent(event);
		}
		
		private function updateBuddyList(event:Event):void{
			_showBuddyList.enabled = false;
			_showCurrUserList.enabled = true;
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.BUDDYLIST);
			dispatchEvent(e);
			
		}
		
		private function getMyInfo(event:MouseEvent):void{
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.GETMYINFO);
			dispatchEvent(e);
		}
		
		private function updateCurrUserList(event:Event):void{
			_showBuddyList.enabled = true;
			_showCurrUserList.enabled = false;
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.CURRENTLIST);
			dispatchEvent(e);
		}
		
		private function chatSend(e:MouseEvent):void{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.CHATSEND);
			event.param = _chatInput.text;
			dispatchEvent(event);
			
			_chatInput.text = "";
			stage.focus = _chatInput;
		}
		
		private function onUserSelect(e:Event):void{
			trace("lobby  user select"+_userTip.x+" "+_userTip.y);
			_userTip.visible = true;
			_userTip.x = this.mouseX;
			_userTip.y = this.mouseY;
			_currentSelectUserName = e.target.selectedItem.data as String;
			_userTip.userInfo.addEventListener(MouseEvent.CLICK,getUserInfo);
			_userTip.sendMessage.addEventListener(MouseEvent.CLICK, sendPrivateMessage);
		}
		
		private function onUserUnSelect(e:MouseEvent):void{
			_userTip.visible = false;
		}
		
		private function getUserInfo(event:Event):void{
			_userTip.visible = false;
			_userTip.userInfo.removeEventListener(MouseEvent.CLICK,getUserInfo);
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.GETUSERINFO);
			e.param = _currentSelectUserName;
			dispatchEvent(e);
		}
		
		private function sendPrivateMessage(e:Event):void{
			_userTip.userInfo.removeEventListener(MouseEvent.CLICK,sendPrivateMessage);
			_userTip.visible = false;
			_chatInput.text = "/"+_currentSelectUserName+":";
			stage.focus = _chatInput;
		}
		
		
		
		private function onRoomSelect(event:Event):void{
			this._joinRoom.enabled = true;
			trace("on room select target:"+event.target.selectedItem.data);
		   // var row:int = event.target.selectedItem.listDate.row;
			//trace("on room select:"+row);
			this._currentSelectGameId = event.target.selectedItem.data[0] as int;
			this._currentSelectGamePassword = event.target.selectedItem.data[1] as Boolean;
			trace(this._currentSelectGameId);
			
			
			
		}
		
		private function onGameSelect(event:Event):void{
			this._quickJoin.enabled = true;
			this._currentSelectGameName = event.target.selectedItem.data as String;
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.GAMESELECT);
			e.param = event.target.selectedItem.data as String;
			dispatchEvent(e);			
		}
		
		private function onQuickJoin(event:MouseEvent):void{
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.QUICKJOIN);
			dispatchEvent(e);
		}
		
		private function onJoinRoom(event:MouseEvent):void{
			if(this._currentSelectGamePassword){
				this._passwordPanel.visible = true;
				this._passwordPanel.password.text = "";
				this._passwordPanel.cancel.addEventListener(MouseEvent.CLICK, removePasswordPanel);
				this._passwordPanel.enter.addEventListener(MouseEvent.CLICK, enterPassword);
			}else{
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.JOINGAME);
			e.param = new Array();
			e.param[0] = this._currentSelectGameId;
			e.param[1] = null;
			dispatchEvent(e);
			}
		}
		
		private function removePasswordPanel(event:MouseEvent):void{
			this._passwordPanel.cancel.removeEventListener(MouseEvent.CLICK, removePasswordPanel);
			this._passwordPanel.password.text = "";
			this._passwordPanel.visible = false;
		}
		
		private function enterPassword(event:MouseEvent):void{
			this._passwordPanel.enter.removeEventListener(MouseEvent.CLICK, enterPassword);
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.JOINGAME);
			e.param = new Array();
			e.param[0] = this._currentSelectGameId;
			e.param[1] = this._passwordPanel.password.text;
			dispatchEvent(e);
			this._passwordPanel.visible = false;
		}
		
		
		private function onCreateRoom(event:MouseEvent):void{
			this._createRoomPanel.visible = true;
			this._createRoomPanel.password.enabled = false;
			this._createRoomPanel.gameName.text = this._currentSelectGameName;
			this._createRoomPanel.roomName.text = "";
			this._createRoomPanel.password.text = "";
			this._createRoomPanel.createBtn.addEventListener(MouseEvent.CLICK, createNewRoom);
			this._createRoomPanel.cancelBtn.addEventListener(MouseEvent.CLICK, closeCreateRoomBtn);
			this._createRoomPanel.passwordEnable.addEventListener(MouseEvent.CLICK,enablePassword);
		}
		
		private function closeCreateRoomBtn(event:MouseEvent):void{
			this._createRoomPanel.createBtn.removeEventListener(MouseEvent.CLICK, createNewRoom);
			this._createRoomPanel.passwordEnable.removeEventListener(MouseEvent.CLICK,enablePassword);
			this._createRoomPanel.cancelBtn.removeEventListener(MouseEvent.CLICK, closeCreateRoomBtn);
			this._createRoomPanel.visible = false;
			_createRoomPasswordEnable = false;
		}
		
		private function enablePassword(event:MouseEvent):void{
			if(_createRoomPasswordEnable == false){
			this._createRoomPanel.password.enabled = true;
			_createRoomPasswordEnable = true;
			}else {
				this._createRoomPanel.password.text = "";
				this._createRoomPanel.password.enabled = false;
				_createRoomPasswordEnable = false;
			}
		}
		
		private function createNewRoom(event:MouseEvent):void{
			this._createRoomPanel.createBtn.removeEventListener(MouseEvent.CLICK, createNewRoom);
			this._createRoomPanel.passwordEnable.removeEventListener(MouseEvent.CLICK,enablePassword);
			this._createRoomPanel.cancelBtn.removeEventListener(MouseEvent.CLICK, closeCreateRoomBtn);
			this._createRoomPanel.visible = false;
			_createRoomPasswordEnable = false;
			if(this._createRoomPanel.roomName.text != null){
				var e:LobbyEvent = new LobbyEvent(LobbyEvent.CREATEGAME);
				var ar:Array = new Array();
				ar[0] = this._createRoomPanel.roomName.text;
				ar[1] = this._createRoomPanel.password.text;
				e.param = ar;
				dispatchEvent(e);
			}
			else 
				trace("room name is null");
		}
		/*
		public static function instance():Lobby{
			if(myLobby == null)
				myLobby = new Lobby();
				myLobby
			return myLobby;
		}
		*/
		public function showUserInfoPanle(userinfo:Array, isme:Boolean, isbuddy:Boolean){
			_userInfoPanel.visible = true;
			_userInfoPanel.username.text = userinfo[0];
			trace("*-----********"+userinfo[1]);
			if(userinfo[1]==0){
				_userInfoPanel.gender.text = "男";
			}else if(userinfo[1] ==1){
				_userInfoPanel.gender.text = "女";
			}
			_userInfoPanel.state.text = userinfo[2];
			if(!isme){
				_userInfoPanel.setInfo.visible = false;
				_userInfoPanel.addBuddy.visible = true;
				_userInfoPanel.removeBuddy.visible = true;
				if(isbuddy){
					_userInfoPanel.removeBuddy.visible = true;
					_userInfoPanel.addBuddy.visible = false;
				}else {
					_userInfoPanel.removeBuddy.visible = false;
					_userInfoPanel.addBuddy.visible = true;
				}
			}else {
				_userInfoPanel.setInfo.visible = true;
				_userInfoPanel.addBuddy.visible = false;
				_userInfoPanel.removeBuddy.visible = false;
			}
			
		}
		
		public function setUserList(dp:DataProvider):void{
			_userList.dataProvider = dp;
		}
		
		public function setGameList(dp:DataProvider):void{
			_zoneList.dataProvider = dp;
		}
		
		public function setRoomList(dp:DataProvider):void{
			_roomList.dataProvider = dp;
			
		}
		
		public function addChat(s:String):void{
			_chatBox.appendText(s);
			_chatBox.appendText('\n');
		}
		
		public function setChatInput(s:String):void{
			_chatInput.text = s;
			stage.focus = _chatInput;
		}
		
		public function showMessagePanel(s:String):void{
			_messagePanel.visible = true;
			_messagePanel.message.text = s;
			_messagePanel.btnclose.addEventListener(MouseEvent.CLICK, messageClose);
			
		}
		
		
		public function destroy():void{
			_userList.removeEventListener(Event.CHANGE, onUserSelect);
			_showBuddyList.removeEventListener(MouseEvent.CLICK, updateBuddyList);
			_showCurrUserList.removeEventListener(MouseEvent.CLICK, updateCurrUserList);
			
			_zoneList.removeEventListener(Event.CHANGE, onGameSelect);
			_roomList.removeEventListener(Event.CHANGE, onRoomSelect);
			
			_quickJoin.removeEventListener(MouseEvent.CLICK, onQuickJoin);
			
			_chatSend.removeEventListener(MouseEvent.CLICK, chatSend);
		}
		
		
		
	}

}